package com.zz.nondatain.repository;

import com.zz.nondatain.network.ApiService;
import com.zz.nondatain.network.BaseResponse;
import com.zz.nondatain.network.RetrofitClient;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableTransformer;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * 所有Repository的基类
 */
public class BaseRepository {
    
    protected ApiService apiService;
    
    public BaseRepository() {
        apiService = RetrofitClient.getInstance().getApiService();
    }
    
    /**
     * 统一线程处理
     */
    protected <T> ObservableTransformer<T, T> applySchedulers() {
        return upstream -> upstream
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    
    /**
     * 处理API响应
     */
    protected <T> Observable<T> handleResponse(Observable<BaseResponse<T>> observable) {
        return observable
                .map(response -> {
                    if (response.isSuccess()) {
                        return response.getData();
                    } else {
                        throw new Exception(response.getMessage());
                    }
                });
    }
} 